home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
54200
/
54200.xpi
/
chrome
/
dogood.jar
/
content
/
DoGoodReplace.js
< prev
next >
Wrap
Text File
|
2010-01-05
|
14KB
|
414 lines
/**
*
* Base script for find banners and replace them.
*
*/
const rCc = Components.classes;
const rCi = Components.interfaces;
const rLoader = rCc["@mozilla.org/moz/jssubscript-loader;1"].getService(rCi.mozIJSSubScriptLoader);
rLoader.loadSubScript('chrome://dogood/content/DoGoodCookie.js');
rLoader.loadSubScript('chrome://dogood/content/DoGoodFilters.js');
rLoader.loadSubScript('chrome://dogood/content/DoGoodReplaceRules.js');
rLoader.loadSubScript('chrome://dogood/content/DoGoodContextMenu.js');
/**
* Class replace.
* Contains methods for searching and replacing banners.
*/
const DoGoodReplace = {
/**
* Address of ads server
*
* @type String
*/
adsserver : 'http://ads.dogoodhq.com/',
/**
* Address of face server
*
* @type String
*/
faceserver : 'http://u.dogoodhq.com/',
/**
* Count of hits for the current session
*
* @type Int
*/
currentCount: null,
/**
* Id of current session
*
* @type Int
*/
sessionId : new Date().getTime(),
/**
* Current version
*
* @type String
*/
version : '1.0.3',
/**
* Function for search image banners.
*
* @param {object} doc - current loaded document
*/
imageBannersSearch : function(doc) {
if (!doc) doc = window.content.document; //if not document, get document current page
var imgs = doc.images;
var l = 0;
var len = imgs.length;
while (l < len) {
if (imgs[l].parentNode.tagName.toLowerCase() == 'a') { //if <img> object in <a> object
var a = imgs[l].parentNode;
var w = imgs[l].naturalWidth || imgs[l].width || imgs[l].offsetWidth;
var h = imgs[l].naturalHeight || imgs[l].height || imgs[l].offsetHeight;
var iname = w + "x" + h;
// if object <a> is not replaced and href in filter list (or src of <img> in filter list)
if (DoGoodRules.newimages[iname] && !a.backsrc && (DoGoodFilter.RegExpFilter(a.href) || DoGoodFilter.RegExpFilter(imgs[l].src))) {
DoGoodReplace.createBanner(doc, a, a.parentNode, 'image', w, h);
}
}
l++;
};
DoGoodReplace.objectBannersSearch(doc); //go to search all other objects
},
/**
* Function for search embed, object and iframe banners.
*
* @param {object} doc - current loaded document
*/
objectBannersSearch : function (doc) {
DoGoodReplace.collectionOverlay(doc, doc.embeds, "embed", "src"); // check all embed objects
DoGoodReplace.collectionOverlay(doc, doc.getElementsByTagName('object'), "object", "data"); // check all <object> objects
//if (!doc.defaultView.frameElement) { // and if loaded root document, check all iframes
DoGoodReplace.collectionOverlay(doc, doc.getElementsByTagName('iframe'), "iframe", "src");
//}
},
/**
* Function for overlay collection objects.
*
* @param {object} doc - current loaded document
* @param {objects collection} collection - collection of objects
* @param {string} type - type of objects
* @param {string} attr - attribute of object for filter check
*/
collectionOverlay : function(doc, collection, type, attr) {
var i = 0;
var len = collection.length;
while (i < len) {
var obj = collection[i];
var w = obj.width;
var h = obj.height;
if (!w) {
w = obj.style.width;
if (w.indexOf('px') != -1) w = w.substr(0, w.indexOf('px'));
h = obj.style.height;
if (h.indexOf('px') != -1) h = h.substr(0, h.indexOf('px'));
}
var iname = w + "x" + h;
var link = obj.getAttribute(attr);
if ((obj.id != "DoGoodObject") && DoGoodRules.newimages[iname] && DoGoodReplace.objectIsBanner(obj, type, link)) {
var prObj = DoGoodReplace.getObjectAndParent(obj, type);
DoGoodReplace.createBanner(doc, prObj.obj, prObj.pr, type, w, h);
if (type == 'iframe') { len--; } else { i++; }
} else {
i++;
}
}
},
/**
* Function to check specific features of object
*
* @param {object} obj - current object
* @param {string} type - type of objects
* @param {string} link - link for filter check
*/
objectIsBanner : function(obj, type, link) {
switch (type)
{
case "embed":
var clickTag = false;
var flashvars = obj.getAttribute("flashvars");
if (flashvars) {
if ((flashvars.toLowerCase().indexOf('clicktag') != -1) || (flashvars.toLowerCase().indexOf('clickurl') != -1)) {
clickTag = true;
}
}
var src = obj.getAttribute("src");
if ((src.toLowerCase().indexOf('clicktag') != -1) || (src.toLowerCase().indexOf('clickurl') != -1)) {
clickTag = true;
}
if ( DoGoodFilter.RegExpFilter(link) || clickTag ) return true;
break;
case "object":
if ( DoGoodFilter.RegExpFilter(link) ) return true;
break;
case "iframe":
if (DoGoodFilter.RegExpFilter(link) && !DoGoodReplace.isDogoodObject(obj.contentWindow.document)) return true;
break;
default: return false;
}
},
/**
* Function for get parent object for current object
*
* @param {object} obj - current object
* @param {string} type - type of objects
*/
getObjectAndParent : function(obj, type) {
var pr = obj.parentNode;
if ((type == "embed") && (obj.parentNode.tagName.toLowerCase() == 'object')) {
pr = pr.parentNode;
obj = obj.parentNode;
}
return { obj: obj, pr: pr }
},
/**
* Create new image and link objects
*
* @param {object} doc - current loaded document
* @param {object} obj - current object
* @param {object} pr - parrent object
* @param {string} type - type of objects
* @param {int} w - width of object
* @param {int} h - height of object
*/
createBanner : function(doc, obj, pr, type, w, h) {
if ((type == 'iframe') || (type == 'image')) {
var newA = doc.createElement("a");
newA.setAttribute("href", "");
newA.setAttribute("target", "_blank");
pr.appendChild(newA);
pr.insertBefore(newA,obj);
var newImage = doc.createElement("img");
newImage.setAttribute("src", "");
newImage.setAttribute("alt", "");
newImage.setAttribute("width", 0);
newImage.setAttribute("height", 0);
newImage.setAttribute("border", 0);
newA.appendChild(newImage);
newA.backsrc = obj;
newA.backw = w;
newA.backh = h;
newImage.setAttribute("id", "DoGoodObject");
DoGoodReplace.replaceAll(newA, newImage);
} else {
var newObj = doc.createElement("object");
newObj.setAttribute("id", "DoGoodObject");
newObj.setAttribute("width", 0);
newObj.setAttribute("height", 0);
newObj.setAttribute("codebase", "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0");
pr.appendChild(newObj);
pr.insertBefore(newObj, obj);
var newEmbed = doc.createElement("embed");
newEmbed.setAttribute("src", "");
newEmbed.setAttribute("width", 0);
newEmbed.setAttribute("height", 0);
newEmbed.setAttribute("pluginspace","HTTP://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash");
newEmbed.setAttribute("type", "application/x-shockwave-flash");
newEmbed.setAttribute("quality", "high");
newEmbed.setAttribute("allowscriptaccess", "never");
newObj.appendChild(newEmbed);
newObj.backsrc = obj;
newObj.backw = w;
newObj.backh = h;
DoGoodReplace.replaceAll(newObj, newEmbed, type);
}
var loc = doc.location.href;
},
/**
* Replace src of image and href of link
*
* @param {object} a - link of banner
* @param {object} img - image of banner
*/
replaceAll : function(a, img, type) {
var w = a.backw;
var h = a.backh;
var iname = w + "x" + h;
var rnd = new Date().getTime();
//code for tracking repeat banners
if (DoGoodRules.newimages[iname][1] == -1) {
DoGoodRules.newimages[iname][1] = Math.floor((19)*Math.random()) + 1;
} else {
if (DoGoodRules.newimages[iname][1] < 20) {
DoGoodRules.newimages[iname][1]++;
} else {
DoGoodRules.newimages[iname][1] = 1;
}
}
if ((type == "embed") || (type == "object")) {
//set src for server swf
img.setAttribute("src", DoGoodReplace.adsserver + "session/" + DoGoodReplace.sessionId + "rnd" + rnd + "swf" + DoGoodRules.newimages[iname][1] + "size" + iname + "/");
//set link in clickTAD parameter
img.setAttribute("flashvars", "clickTAG=" + DoGoodReplace.adsserver + "session/" + DoGoodReplace.sessionId + "rnd" + rnd + "swf" + DoGoodRules.newimages[iname][1] + "hrefsize" + iname + "/");
img.width = w;
img.height = h;
a.width = w;
a.height = h;
} else {
a.target = "_blank";
//set link for server script
a.setAttribute("href",DoGoodReplace.adsserver + "session/" + DoGoodReplace.sessionId + "rnd" + rnd + "img" + DoGoodRules.newimages[iname][1] + "hrefsize" + iname + "/");
//set src for server script
img.setAttribute("src", DoGoodReplace.adsserver + "session/" + DoGoodReplace.sessionId + "rnd" + rnd + "img" + DoGoodRules.newimages[iname][1] + "size" + iname + "/");
img.width = w;
img.height = h;
}
a.backsrc.parentNode.removeChild(a.backsrc);
DoGoodReplace.currentCount++;
document.getElementById('status-count').value = DoGoodReplace.currentCount; //set to bottom right pannel current count
},
/**
* Function to check the availability of replacements in the document
*
* @param {object} doc - current document
*/
isDogoodObject : function(doc) {
if (doc.getElementById('DoGoodObject')) return true;
var iframes = doc.getElementsByTagName("iframe");
for (var k=0;k<iframes.length;k++) {
if (iframes[k].contentWindow.document.getElementById('DoGoodObject')) {
return true;
DoGoodReplace.isDogoodObject(iframes[k].contentWindow.document);
}
}
return false;
},
/**
* Function for check site ignore and start replace
*
* @param {object} doc - current loaded document
*/
startReplace : function(doc) {
if (!doc) doc = window.content.document;
var loc = doc.location.href;
var rootDoc = doc;
if (doc.defaultView.frameElement) {
rootDoc = doc.defaultView.top.document;
loc = rootDoc.location.href;
}
if (!DoGoodCookie.isIgnore(loc, rootDoc)) {
DoGoodReplace.imageBannersSearch(doc);
}
}
};
/**
* Class DoGoodLoad.
* Contains methods for initializing addon
*/
var DoGoodLoad = {
init: function() {
var appcontent = document.getElementById("appcontent");
if(appcontent) { //if browser is ready, set DOMContentLoaded event
appcontent.addEventListener("DOMContentLoaded", DoGoodLoad.onPageLoad, true);
//DoGoodLoad.checkVersion();
}
},
onPageLoad: function(aEvent) {
//reading cookie and redirect on thank you page
if ((DoGoodCookie.readCookie('DoGood_version') == DoGoodReplace.version) && (DoGoodCookie.readCookie('DoGood_redirect') == 'no')) {
DoGoodCookie.createCookie('DoGood_redirect','yes',365);
gBrowser.selectedTab = gBrowser.addTab(DoGoodReplace.faceserver+"thanks/");
}
//init context menu elements
if (!DoGoodContextMenu.context_menu) {
DoGoodContextMenu.context_menu_showorigall = document.getElementById('context-dogood-showorigall');
DoGoodContextMenu.context_menu_report = document.getElementById('context-dogood-report');
DoGoodContextMenu.context_menu_ignoreon = document.getElementById('context-dogood-ignoreon');
DoGoodContextMenu.context_menu_ignoreoff = document.getElementById('context-dogood-ignoreoff');
DoGoodContextMenu.context_menu = document.getElementById("contentAreaContextMenu");
DoGoodContextMenu.context_menu.addEventListener("popupshowing", DoGoodContextMenu.DoGoodContextOn, true);
}
//init toolbar elements
if (document.getElementById('toolbarContextMenu') && !DoGoodContextMenu.toolbar_menu) {
DoGoodContextMenu.toolbar_menu = document.getElementById('toolbarContextMenu');
DoGoodContextMenu.toolbar_menu_showorigall = document.getElementById('toolbar-dogood-showorigall');
DoGoodContextMenu.toolbar_menu_ignoreon = document.getElementById('toolbar-dogood-ignoreon');
DoGoodContextMenu.toolbar_menu_ignoreoff = document.getElementById('toolbar-dogood-ignoreoff');
DoGoodContextMenu.toolbar_menu.addEventListener("popupshowing", DoGoodContextMenu.DoGoodToolbarAction, true);
}
//if server is not down, begin replacing
if (DoGoodFilter.filtersText) {
var doc = aEvent.originalTarget;
DoGoodReplace.startReplace(doc);
}
},
tabSelected : function(aEvent) {
},
windowClose : function(aEvent) {
var xmlhttp = DoGoodFilter.getXmlHttp();
xmlhttp.open('GET', DoGoodReplace.adsserver+'server/clearcache/?sessionId='+DoGoodReplace.sessionId, true);
xmlhttp.send(null);
},
checkVersion : function() {
if (!DoGoodCookie.readCookie('DoGooderIsUpdate')) {
var xmlhttp = DoGoodFilter.getXmlHttp();
xmlhttp.open('GET', DoGoodReplace.faceserver+'server/checkversion/?browser=firefox', true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
var req_version = xmlhttp.responseText;
if (req_version && (DoGoodReplace.version != req_version)) {
gBrowser.selectedTab = gBrowser.addTab(DoGoodReplace.faceserver+"updatenow/");
}
}
}
};
xmlhttp.send(null);
DoGoodCookie.createCookie('DoGooderIsUpdate','yes', 1);
}
}
}
DoGoodFilter.getFilters();
window.addEventListener("load", function() { DoGoodLoad.init(); }, false);
window.addEventListener("unload", function() { DoGoodLoad.windowClose(); }, false);